/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.service;

import java.util.List;

import com.jeesite.modules.st.utils.BillUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.st.entity.StSaleBill;
import com.jeesite.modules.st.dao.StSaleBillDao;
import com.jeesite.modules.st.entity.StSaleBillDetail;
import com.jeesite.modules.st.dao.StSaleBillDetailDao;

import javax.annotation.Resource;

/**
 * 销售单Service
 * @author HOHD
 * @version 2020-12-18
 */
@Service
@Transactional(readOnly=true)
public class StSaleBillService extends CrudService<StSaleBillDao, StSaleBill> {
	
	@Autowired
	private StSaleBillDetailDao stSaleBillDetailDao;

	public List<StSaleBillDetail> findDetail(StSaleBill stSaleBill) {
		return stSaleBillDetailDao.findDetail(stSaleBill.getId().split(","));
	}
	public Page<StSaleBillDetail> showDetail(StSaleBillDetail stSaleBillDetail) {
		Page<StSaleBillDetail> page = (Page<StSaleBillDetail>) stSaleBillDetail.getPage();
//	page.setPageSize(Page.PAGE_SIZE_NOT_PAGING); 	// 不分页，返回全部数据。
//	page.setCount(Page.COUNT_NOT_COUNT);  			// 不执行Count，不查询总数。
//	page.setCount(Page.COUNT_ONLY_COUNT); 			// 只Count数据，不返回数据，通过 page.getCount()获取
		page.setList(stSaleBillDetailDao.showDetail(stSaleBillDetail));
		return page;
	}
	public Page<StSaleBillDetail> listSaleDetail(StSaleBillDetail stSaleBillDetail) {
		Page<StSaleBillDetail> page = (Page<StSaleBillDetail>) stSaleBillDetail.getPage();
//	page.setPageSize(Page.PAGE_SIZE_NOT_PAGING); 	// 不分页，返回全部数据。
//	page.setCount(Page.COUNT_NOT_COUNT);  			// 不执行Count，不查询总数。
//	page.setCount(Page.COUNT_ONLY_COUNT); 			// 只Count数据，不返回数据，通过 page.getCount()获取
		page.setList(stSaleBillDetailDao.listSaleDetail(stSaleBillDetail));
		return page;
	}
	public Page<StSaleBillDetail> saleStatistics(StSaleBillDetail stSaleBillDetail) {
		Page<StSaleBillDetail> page = (Page<StSaleBillDetail>) stSaleBillDetail.getPage();
//	page.setPageSize(Page.PAGE_SIZE_NOT_PAGING); 	// 不分页，返回全部数据。
//	page.setCount(Page.COUNT_NOT_COUNT);  			// 不执行Count，不查询总数。
//	page.setCount(Page.COUNT_ONLY_COUNT); 			// 只Count数据，不返回数据，通过 page.getCount()获取
		page.setList(stSaleBillDetailDao.saleStatistics(stSaleBillDetail));
		return page;
	}
	/**
	 * 获取单条数据
	 * @param stSaleBill
	 * @return
	 */
	@Override
	public StSaleBill get(StSaleBill stSaleBill) {
		StSaleBill entity = super.get(stSaleBill);
		if (entity != null){
			StSaleBillDetail stSaleBillDetail = new StSaleBillDetail(entity);
			stSaleBillDetail.setStatus(StSaleBillDetail.STATUS_NORMAL);
			entity.setStSaleBillDetailList(stSaleBillDetailDao.findList(stSaleBillDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param stSaleBill 查询条件
	 * @return
	 */
	@Override
	public Page<StSaleBill> findPage(StSaleBill stSaleBill) {
		return super.findPage(stSaleBill);
	}
	@Resource
	BillUtil billUtil;
	/**
	 * 保存数据（插入或更新）
	 * @param stSaleBill
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(StSaleBill stSaleBill) {
		if (stSaleBill.getIsNewRecord()) {
			String billNO = billUtil.billNo(BillUtil.BillType.saleBill);
			stSaleBill.setBillNo(billNO);
		}
		super.save(stSaleBill);
		// 保存 StSaleBill子表
		for (StSaleBillDetail stSaleBillDetail : stSaleBill.getStSaleBillDetailList()){
			if (!StSaleBillDetail.STATUS_DELETE.equals(stSaleBillDetail.getStatus())){
				stSaleBillDetail.setParentId(stSaleBill);
				if (stSaleBillDetail.getIsNewRecord()){
					stSaleBillDetailDao.insert(stSaleBillDetail);
				}else{
					stSaleBillDetailDao.update(stSaleBillDetail);
				}
			}else{
				stSaleBillDetailDao.delete(stSaleBillDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param stSaleBill
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(StSaleBill stSaleBill) {
		super.updateStatus(stSaleBill);
	}
	
	/**
	 * 删除数据
	 * @param stSaleBill
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(StSaleBill stSaleBill) {
		super.delete(stSaleBill);
		StSaleBillDetail stSaleBillDetail = new StSaleBillDetail();
		stSaleBillDetail.setParentId(stSaleBill);
		stSaleBillDetailDao.deleteByEntity(stSaleBillDetail);
	}
	
}